package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.SLADetailBean;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangqi on 2018/1/30.
 */

public class WorkDetailSlaActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_response_state)
    TextView tvResponseState;
    @Bind(R.id.tv_response_time)
    TextView tvResponseTime;
    @Bind(R.id.tv_response_use)
    TextView tvResponseUse;
    @Bind(R.id.tv_arrive_state)
    TextView tvArriveState;
    @Bind(R.id.tv_arrive_time)
    TextView tvArriveTime;
    @Bind(R.id.tv_arrive_use)
    TextView tvArriveUse;
    @Bind(R.id.tv_deal_state)
    TextView tvDealState;
    @Bind(R.id.tv_deal_time)
    TextView tvDealTime;
    @Bind(R.id.tv_deal_use)
    TextView tvDealUse;
    @Bind(R.id.btn_sla)
    Button btnSla;
    @Bind(R.id.rl_response)
    RelativeLayout rlResponse;
    @Bind(R.id.rl_arrive)
    RelativeLayout rlArrive;
    @Bind(R.id.rl_deal)
    RelativeLayout rlDeal;
    private String requestId;
    private String outsourceFlag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_detail_sla);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        requestId = getIntent().getStringExtra("requestId");
        outsourceFlag = getIntent().getStringExtra("outsourceFlag");

        simpleTileView.setTitle("SLA");
        btnSla.setOnClickListener(this);
        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("requestId", requestId);
        ServiceRequestManager.getManager().getSlaCompliacRate(this, outsourceFlag, JsonMapUtil.mapToJson(map), this);
    }

    private void setData(SLADetailBean.RowsBean bean) {
        if ("1".equals(bean.getIsSlaResponse())) {
            tvResponseState.setText("响应达标");
            tvResponseState.setBackgroundResource(R.drawable.shape_card_76c56d);
            tvResponseUse.setTextColor(getResources().getColor(R.color.color_333333));
        } else if ("0".equals(bean.getIsSlaResponse())) {
            tvResponseState.setText("响应未达标");
            tvResponseState.setBackgroundResource(R.drawable.shape_card_e64545);
            tvResponseUse.setTextColor(getResources().getColor(R.color.color_red_bar));
        } else {
            rlResponse.setVisibility(View.GONE);
        }
        if ("1".equals(bean.getIsSlaArrive())) {
            tvArriveState.setText("到场达标");
            tvArriveState.setBackgroundResource(R.drawable.shape_card_76c56d);
            tvArriveUse.setTextColor(getResources().getColor(R.color.color_333333));
        } else if ("0".equals(bean.getIsSlaArrive())) {
            tvArriveState.setText("到场未达标");
            tvArriveState.setBackgroundResource(R.drawable.shape_card_e64545);
            tvArriveUse.setTextColor(getResources().getColor(R.color.color_red_bar));
        } else {
            rlArrive.setVisibility(View.GONE);
        }
        if ("1".equals(bean.getIsSlaDealt())) {
            tvDealState.setText("处理达标");
            tvDealState.setBackgroundResource(R.drawable.shape_card_76c56d);
            tvDealUse.setTextColor(getResources().getColor(R.color.color_333333));
        } else if ("0".equals(bean.getIsSlaDealt())) {
            tvDealState.setText("处理未达标");
            tvDealState.setBackgroundResource(R.drawable.shape_card_e64545);
            tvDealUse.setTextColor(getResources().getColor(R.color.color_red_bar));
        } else {
            rlDeal.setVisibility(View.GONE);
        }

        tvResponseTime.setText(formatTime(bean.getSlaResponses()));
        tvResponseUse.setText(formatTime(bean.getSlaResponse()));

        tvArriveTime.setText(formatTime(bean.getSlaArrival()));
        tvArriveUse.setText(formatTime(bean.getSlaArrive()));

        tvDealTime.setText(formatTime(bean.getSlaComplete()));
        tvDealUse.setText(formatTime(bean.getSlaDealt()));

    }

    private String formatTime(int min) {
        int h = min / 60;
        int m = min % 60;
        if (h != 0) {
            return h + "小时" + m + "分钟";
        } else {
            return m + "分钟";
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_sla:
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                SLADetailBean bean = new Gson().fromJson(result, SLADetailBean.class);
                setData(bean.getRows());
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }

    public static void OpenActivity(Context context, String requestId, String outsourceFlag) {
        Intent intent = new Intent(context, WorkDetailSlaActivity.class);
        intent.putExtra("requestId", requestId);
        intent.putExtra("outsourceFlag", outsourceFlag);
        context.startActivity(intent);
    }
}
